package com.sunnada.edu.system.sysrole.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnada.edu.system.pub.common.Common;
import com.sunnada.edu.system.sysrole.dao.RoleDao;
import com.sunnada.edu.system.sysuser.dao.SysUserDao;



@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	@Resource
	RoleDao roleDao;
	@Resource
	SysUserDao userDao;
	@Override
	public int getRoleCots(HashMap<String, String> params) {
		return roleDao.getRoleCots(params);
	}
	@Override
	public List<?> findRoles(HashMap<String, String> params) {
		return roleDao.findRoles(params);
	}
	@Override
	public String roleAdd(HashMap<String, String> params) {
		String msg = "";
		String recId =UUID.randomUUID().toString();
		params.put("recId", recId);
		int num = roleDao.roleAdd(params);
		//新增一个角色，默认都会赋给超级管理员账号：admin，agent和loginUserId(自己)
		RoleMethods.grentRoleToAdmin(params);
		if(num==0){
			msg = "添加失败!";
		}else{
			moduleRoleAdds(params, recId);
		}
		return msg;
	}
	private String moduleRoleAdds(HashMap<String, String> params, String recId) {
		String moduleIds = params.get("moduleIds");
		if(moduleIds!=null){
			String s[] = moduleIds.split(",");
			for(int i=0;i<s.length;i++){
				HashMap<String, String> param = new HashMap<String, String>();
				param.put("loginUserId",params.get("loginUserId"));
				param.put("roleId",recId);
				param.put("moduleId",s[i]);
				param.put("recId",UUID.randomUUID().toString());
				moduleRoleAdd(param);
			}
		}
		return null;
	}
	
	@Override
	public String moduleRoleAdd(HashMap<String, String> params) {
		int num = roleDao.moduleRoleAdd(params);
		if(num==0){
			return "保存失败！";
		}
		return null;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public Map getRoleByRecId(HashMap<String, String> params) {
		return roleDao.getRoleByRecId(params);
	}

	@Override
	public String roleUpdate(HashMap<String, String> params) {
		String msg = "";
		int num = roleDao.roleUpdate(params);
		if(num==0){
			msg = "修改失败!";
		}else{
			msg = moduleRoleDelete(params);
			if(msg==null){
			 msg = moduleRoleAdds(params, params.get("recId"));
			}			
		}
		return msg;		
	}
	
	@Override
	public String moduleRoleDelete(HashMap<String, String> params) {
		int num =  roleDao.moduleRoleDelete(params);
		return null;
	}
	@Override
	public String roleDel(HashMap<String, String> params) {
		String msg = null;
		//List list=RoleMethods.getRoleUsedList(params);
		int num = roleDao.roleDel(params);
		if(num==0){
			msg = "删除失败!";
		}else{
			msg = moduleRoleDelete(params);
			RoleMethods.deleteUserRole(params);
		}
		return msg;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	



	
	@Override
	public String[] getModuleIdsByRoleId(HashMap<String, String> params) {
		return roleDao.getModuleIdsByRoleId(params);
	}
	@Override
	public List roleTree2(HashMap<String, String> params) {
		return roleDao.roleTree2(params);
	}
	
	@Override
	public int getRoleUserCots(HashMap<String, String> params) {		
		return roleDao.getRoleUserCots(params);
	}
	@Override
	public List<?> findRoleUsers(HashMap<String, String> params) {
		return roleDao.findRoleUsers(params);
	}
	@Override
	public int getUserNoexistRoleIdCots(HashMap<String, String> params) {	
		return roleDao.getUserNoexistRoleIdCots(params);
	}
	@Override
	public List<?> findUserNoexistRoleIds(HashMap<String, String> params) {
		return roleDao.findUserNoexistRoleIds(params);
	}
	@Override
	public String roleBindSave(HashMap<String, String> params) {
		String userIds = params.get("userIds");  //用户id
		if(userIds!=null){
			String s[] = userIds.split(",");
			for(int i=0;i<s.length;i++){
				/*HashMap<String, String> param = new HashMap<String, String>();*/
				params.put("userId",s[i]);
			    roleUserAdd(params);
			}
		}
		return null;
	}
	@Override
	public String roleUserAdd(HashMap<String, String> params) {
	    int num=roleDao.roleBindSave(params);
	    if (num==0) {
	    	return "角色绑定失败！";
		}
		return null;
	}
	@Override
	public String roleBindDelete(HashMap<String, String> params) {
		String msg=null;
		int num=roleDao.roleBindDelete(params);
		if(num==0){
		    msg="角色解绑失败！";
		}
		return msg;		
	}
	@Override
	public String roleEnable(HashMap<String, String> params) {
		String msg=null;
		int num=roleDao.roleEnable(params);
		if (num==0) {
			msg="角色启用失败！";
		}
		return msg;
	}
	@Override
	public String roleDisable(HashMap<String, String> params) {
		String msg=null;
		int num=roleDao.roleDisable(params);
		if (num==0) {
			msg="角色停用失败！";
		}
		return msg;
	}
	@Override
	public List roleAuthorizeTree(HashMap<String, String> params) {
		return roleDao.roleAuthorizeTree(params);
		
	}
}
